package com.atguigu.gmall.product.service.impl;


import com.atguigu.gmall.model.product.BaseCategoryTrademark;
import com.atguigu.gmall.model.product.BaseTrademark;
import com.atguigu.gmall.model.product.CategoryTrademarkVo;
import com.atguigu.gmall.product.mapper.BaseCategoryTrademarkMapper;
import com.atguigu.gmall.product.mapper.BaseTrademarkMapper;
import com.atguigu.gmall.product.service.BaseCategoryTrademarkService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class BaseCategoryTrademarkMapperImpl extends ServiceImpl<BaseCategoryTrademarkMapper, BaseCategoryTrademark> implements BaseCategoryTrademarkService {

    @Autowired
    private BaseTrademarkMapper baseTrademarkMapper;

    @Autowired
    private BaseCategoryTrademarkMapper baseCategoryTrademarkMapper;

    @Override
    public List<BaseTrademark> findTrademarkList(Long category3Id) {
        //参数只有 category3Id 只能先获取base_category_teademark 表中的trademark_id
        // select * from base_category_trademark where category3_id = category3Id;

        QueryWrapper<BaseCategoryTrademark> baseCategoryTrademarkQueryWrapper = new QueryWrapper<>();
        baseCategoryTrademarkQueryWrapper.eq("category3_id", category3Id);
        List<BaseCategoryTrademark> baseCategoryTrademarkList = baseCategoryTrademarkMapper.selectList(baseCategoryTrademarkQueryWrapper);

        //只需要品牌Id trademark_id
        //映射处理
        if (!CollectionUtils.isEmpty(baseCategoryTrademarkList)) {
            List<Long> idList = baseCategoryTrademarkList.stream().map(baseCategoryTrademark -> {
                //标识获取品牌id
                return baseCategoryTrademark.getTrademarkId();
            }).collect(Collectors.toList());

            //根据这个idList获取到base_trademark;
            return this.baseTrademarkMapper.selectBatchIds(idList);
        }
        //默认返回
        return null;
    }

    @Override
    public List<BaseTrademark> findCurrentTrademarkList(Long category3Id) {
        //两个集合获取差集
        //第一个集合获取所有的品牌数据，获取品牌所对应的Id
        QueryWrapper<BaseCategoryTrademark> baseCategoryTrademarkQueryWrapper = new QueryWrapper<>();
        baseCategoryTrademarkQueryWrapper.eq("category3_id", category3Id);
        List<BaseCategoryTrademark> baseCategoryTrademarkList = baseCategoryTrademarkMapper.selectList(baseCategoryTrademarkQueryWrapper);

        //第一个集合，获取已经绑定好的品牌
        if (!CollectionUtils.isEmpty(baseCategoryTrademarkList)) {
            //根据category3 Id
            //获取到品牌Id trademark id
            List<Long> idList = baseCategoryTrademarkList.stream().map(baseCategoryTrademark -> {
                return baseCategoryTrademark.getTrademarkId();
            }).collect(Collectors.toList());
            //获取品牌所对应的Id
            List<BaseTrademark> baseTrademarkList = baseTrademarkMapper.selectList(null).stream().filter(baseTrademark -> {
                //获取所有：select *from base trademark1,2,3,5,6
                return !idList.contains(baseTrademark.getId());
            }).collect(Collectors.toList());
            //返回 可选 未绑定的
            return baseTrademarkList;
        }
        //没有绑定，查询所有的品牌集合数据
        return baseTrademarkMapper.selectList(null);
    }

    @Override
    public void save(CategoryTrademarkVo categoryTrademarkVo) {
/*        //创建对象
        //单个上传
        categoryTrademarkVo.getTrademarkIdList().forEach(trademarkId -> {
            BaseCategoryTrademark baseCategoryTrademark = new BaseCategoryTrademark();
            baseCategoryTrademark.setCategory3Id(categoryTrademarkVo.getCategory3Id());
            baseCategoryTrademark.setTrademarkId(trademarkId);
            baseCategoryTrademarkMapper.insert(baseCategoryTrademark);
        });*/

        List<BaseCategoryTrademark> baseCategoryTrademarkList = categoryTrademarkVo.getTrademarkIdList().stream().map(trademarkId -> {
            BaseCategoryTrademark baseCategoryTrademark = new BaseCategoryTrademark();
            //  赋值三级分类Id
            baseCategoryTrademark.setCategory3Id(categoryTrademarkVo.getCategory3Id());
            //  赋值品牌Id
            baseCategoryTrademark.setTrademarkId(trademarkId);
            //  返回一个对象
            return baseCategoryTrademark;
        }).collect(Collectors.toList());

        //  批量插入数据！
        this.saveBatch(baseCategoryTrademarkList);
    }

    @Override
    public void removeBaseCategory3IdAndtradeMarkId(Long category3Id, Long trademarkId) {
        //  本质执行更新数据 ：逻辑删除！
        QueryWrapper<BaseCategoryTrademark> baseCategoryTrademarkQueryWrapper = new QueryWrapper<>();
        baseCategoryTrademarkQueryWrapper.eq("category3_id",category3Id);
        baseCategoryTrademarkQueryWrapper.eq("trademark_id",trademarkId);
        this.baseCategoryTrademarkMapper.delete(baseCategoryTrademarkQueryWrapper);

    }


}
